package com.zmn.mcc.services.impl.staff.subarea;

import com.zmn.base.plat.engine.common.constant.PlatEngineConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.model.entity.staff.subarea.McSubAreaRelate;
import com.zmn.mcc.model.entity.staff.subarea.McSubAreaRelateQuery;
import com.zmn.mcc.persistence.interfaces.staff.subarea.McSubAreaRelateDao;
import com.zmn.mcc.services.interfaces.subarea.McSubAreaRelateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 描述: 员工-片区-关联表接口实现类
 *
 * @author jiafei
 * @since 2022/07/18 14:22
 */
@Slf4j
@Service
public class McSubAreaRelateServiceImpl implements McSubAreaRelateService {

    private static final String TAG = "员工-片区-关联表基础服务接口";

    @Resource
    private McSubAreaRelateDao mcSubAreaRelateDao;

    @Override
    public McSubAreaRelate findById(Integer relateId) {

        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id查询数据relateId为null", TAG);
            return null;
        }
        return mcSubAreaRelateDao.findById(relateId);
    }

    @Override
    public List<McSubAreaRelate> listByIds(List<Integer> relateIds) {

        if (CollectionUtils.isEmpty(relateIds)) {
            log.error("[{}],按条件查询数据,relateIds为null", TAG);
            return Collections.emptyList();
        }
        return mcSubAreaRelateDao.listByIds(relateIds);
    }

    @Override
    public List<McSubAreaRelate> listByQuery(McSubAreaRelateQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        return mcSubAreaRelateDao.listByQuery(query);
    }

    @Override
    public List<McSubAreaRelate> listPageByQuery(McSubAreaRelateQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        query.setTotalCount(mcSubAreaRelateDao.countByQuery(query));
        return mcSubAreaRelateDao.listPageByQuery(query);
    }

    @Override
    public Integer countByQuery(McSubAreaRelateQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return mcSubAreaRelateDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(McSubAreaRelate mcSubAreaRelate) {

        if (Objects.isNull(mcSubAreaRelate.getRelateId())) {
            log.error("[{}],根据id修改数据,mcSubAreaRelate为null", TAG);
            return null;
        }
        return mcSubAreaRelateDao.updateByKey(mcSubAreaRelate);
    }

    @Override
    public void updateStatusBatch(List<Integer> ids, Integer status, String operator) {

        if (!Objects.equals(status, PlatEngineConsts.NO) && !Objects.equals(status, PlatEngineConsts.YES)) {
            log.error("[{}],status状态超出取值范围", TAG);
            return;
        }
        mcSubAreaRelateDao.updateStatusBatch(ids, status, operator, DateUtil.getNow());
    }

    @Override
    public Integer insert(McSubAreaRelate mcSubAreaRelate) {

        if (Objects.isNull(mcSubAreaRelate)) {
            log.error("[{}],新增数据,mcSubAreaRelate为null", TAG);
            return null;
        }
        return mcSubAreaRelateDao.insert(mcSubAreaRelate);
    }

    @Override
    public void insertBatch(List<McSubAreaRelate> list) {

        if (CollectionUtils.isEmpty(list)) {
            log.error("[{}],数据为空", TAG);
            return;
        }
        mcSubAreaRelateDao.insertBatch(list);
    }

    @Override
    public Integer deleteByKey(Integer relateId) {

        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id删除数据,relateId为null", TAG);
            return null;
        }
        return mcSubAreaRelateDao.deleteByKey(relateId);
    }

    @Override
    public McSubAreaRelate findByStaffId(Integer staffId) {
        if (Objects.isNull(staffId)) {
            log.error("[{}],根据id查询数据relateId为null", TAG);
            return null;
        }
        return Optional.ofNullable(mcSubAreaRelateDao.findByStaffId(staffId)).orElse(new McSubAreaRelate());
    }

}